package Model;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Database.*;
import Helper.*;

public class VendorM extends Model{
    private ArrayList<Vendor> cache = null;

    public Vendor login(String name, String password){
        VendorM vendorM = new VendorM();
        ArrayList<Vendor> temp = vendorM.find(name);

        Vendor result = null;

        if( temp.size() == 1 ){
            Vendor v = temp.get(0);

            if( v.name.equals(name) && v.getPassword().equals(Helper.md5(password)) ){
                v.setStatus(name, Helper.md5(password));

                result = v;
            }else{
                System.err.println("用户名或密码错误！");
            }
        }else{
            System.err.println("用户名不存在！");
        }

        return result;
    }

    public boolean signup(String name, String password){
        Vendor v = new Vendor(name,Helper.md5(password));

        VendorM vendorM = new VendorM();

        return vendorM.add(v);
    }

    private boolean add(Vendor v){
        VendorDB db = new VendorDB();

        return db.insert(v);
    }

    private ArrayList<Vendor> find(String name){
        VendorDB db = new VendorDB();

        CachedRowSetImpl res = db.match(name);

        return createvendorArr(res);
    }

    protected void init(String tableName){
        // override
        VendorDB db = new VendorDB();

        CachedRowSetImpl res = db.get(tableName);

        cache = createvendorArr(res);
    }

    private ArrayList<Vendor> createvendorArr(CachedRowSetImpl res){
        // override

        ArrayList<Vendor> result = new ArrayList<>();

        try {
            while (res.next()) {
                Vendor u = new Vendor(res.getString(3),res.getString(4));
                u.id = res.getInt(1);

                result.add(u);
            }
        } catch (SQLException exception) {
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }

        return result;
    }
}

class VendorDB extends DB{
    boolean insert(Vendor v){
        int result = -1;

        try{
            connect();

            String sql = "INSERT INTO tb_account(name,password,auth) VALUES(?,?,1)";
            PreparedStatement statement = dbConnection.prepareStatement(sql);
            statement.setString(1,v.name);
            statement.setString(2,v.getPassword());

            result = statement.executeUpdate();
        }catch(SQLException exception){
            System.err.println("用户已存在！或其他数据库错误");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }finally {
            close();
        }

        if( result >= 0 ){
            return true;
        }else{
            return false;
        }
    }

    CachedRowSetImpl match(String name){
        CachedRowSetImpl cachedRowSet = null;

        try{
            connect();

            String sql = "SELECT * FROM tb_account WHERE name=? AND auth=1";
            PreparedStatement statement = dbConnection.prepareStatement(sql);
            statement.setString(1, name);

            ResultSet res = statement.executeQuery();

            cachedRowSet = new CachedRowSetImpl();
            cachedRowSet.populate(res);
        }catch(SQLException exception){
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }finally {
            close();
        }

        return cachedRowSet;
    }
}

